/**
*  \file timex.h
*
*  \brief Date and time conversion.
*
*  \author   Michael J. Hohmannn <Michael.Hohmann@linde-mh.de>
*
*  The module timex declares several types and a function for manipulating time.
*  The function deal with a calendar time that represents the current
*  date (according to the Gregorian calendar) and time. 
*  
*/

#ifndef TIMEX_H_INCLUDED
#define TIMEX_H_INCLUDED

/*================================================[ public includes  ]================================================*/
#include "stm32f10x.h"
#include "global.h"
#include "var_tbl.h"
#include "swidft.h"
/*================================================[ public defines   ]================================================*/

/*================================================[ public datatypes ]================================================*/

/* Calendar time.
*  
*  The type timex_t is a substitute for the time_t type defined by ANSI compliant compilers in <time.h>.
*  Against the ANSI definition this is an unsigned type, which allows the representation of
*  dates later than 2038.
*/ 
typedef U32 timex_t;

/** Broken-down time.
* The struct timex_date hold all members required by the ANSI, to represent broken-down times.
* Unlike the struct tm (defined in <time.h>) is the precision of the members not always signed int.
**/ 
struct timex_date
{
   U8 tm_year;  /*< years since 1900          [0,200] */
   U8 tm_mon;   /*< months since January      [0,11]  */
   U8 tm_mday;  /*< day of month              [1,31]  */
   U8 tm_hour;  /*< hours since midnight      [0,23]  */
   U8 tm_min;   /*< minutes after the hour    [0,59]  */
   U8 tm_sec;   /*< seconds after the minute  [0,60]  */
   U8 tm_wday;  /*< days since Sunday         [0,6]   */
   U16 tm_yday; /*< days since January 1      [0,366] */
   S16 tm_isdst;/*< Daylight Saving Time              */
};

/*================================================[ public variables ]================================================*/

/*================================================[ public functions ]================================================*/
/** Calculate broken-down time.
*  The function timex_TimeToDate converts a passed calendar time into a broken down time.
*  It is designed as a wrapper for the function gmtime() provided by ANSI compliant compilers in <time.h>.
*  There are some differences to the ANSI implementation, beneficial for the use in embedded applications.
*  Particularly the type of the calendar type is unsigned which enables calendar times later than 2038.
*  The allowed range is 1970-01-01 to 2099-12-31, or 0 to 4102444799 in seconds.
*    
*  \param time current calendar time (seconds since 1970-1-1 )
*  \param pDate pointer to an existing timex_date struct, where the calculated broken-down time is stored.
*  
*  \return - RC_ERROR_NULL  if pointer pDate was NIL.
* 		   - RC_ERROR_RANGE if passed time is out of range.
*          - RC_SUCCESS     if time conversion was successful.
* 
*/
RC timex_TimeToDate( timex_t time, struct timex_date *pDate );

RC timex_DateToTime( struct timex_date const *pDate, timex_t *p_time );
#define STATIC_INLINE   static inline
typedef unsigned char BO;
#define NIL ((void*) 0 )

/*====================================================================================================================*/


#endif

/***********************************************************************************************************************
*                             Copyright 2007 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/
